package com.heima.freemarker.controller;

import com.heima.freemarker.pojo.Stutent;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class HelloController {
  @GetMapping("/basic")
    public String test(Model model){

       //纯文本形式的参数
      model.addAttribute("name","freemarker");
      //实体类相关参数
    Stutent stutent = new Stutent();
    stutent.setName("小明");
    stutent.setAge(18);
    model.addAttribute("stu",stutent);
    return  "01-basic";
  }
  @GetMapping("/list")
  public String list(Model model) {
    //------------------------------------
    Stutent stu1 = new Stutent();
    stu1.setName("小强");
    stu1.setAge(18);
    stu1.setMoney(1000.86f);
    stu1.setBirthday(new Date());

    //小红对象模型数据
    Stutent stu2 = new Stutent();
    stu2.setName("小红");
    stu2.setMoney(200.1f);
    stu2.setAge(19);
    //将两个对象模型数据存放到List集合中
    List<Stutent>  stus = new ArrayList<>();
    stus.add(stu1);
    stus.add(stu2);
    //向model中存放List集合数据
    model.addAttribute("stus",stus);
    //创建Map数据
    HashMap<String,Stutent> stuMap = new HashMap<>();
    stuMap.put("stu1",stu1);
    stuMap.put("stu2",stu2);
    // 3.1 向model中存放Map数据
    model.addAttribute("stuMap",stuMap);
    return "02-list";
  }
  @GetMapping("operation")
  public String testOperation(Model model) {
    //构建 Date 数据
    Date now = new Date();
    model.addAttribute("date1", now);
    model.addAttribute("date2", now);

    return "03-operation";
  }
  @GetMapping("innerFunc")
  public String testInnerFunc(Model model) {
    Stutent stu1 = new Stutent();
    stu1.setName("小强");
    stu1.setAge(18);
    stu1.setMoney(1000.86f);
    stu1.setBirthday(new Date());

    //小红对象模型数据
    Stutent stu2 = new Stutent();
    stu2.setName("小红");
    stu2.setMoney(200.1f);
    stu2.setAge(19);
    //1.3 将两个对象模型数据存放到List集合中
    List<Stutent> stus = new ArrayList<>();
    stus.add(stu1);
    stus.add(stu2);
    model.addAttribute("stus", stus);
    // 2.1 添加日期
    Date date = new Date();
    model.addAttribute("today", date);
    // 3.1 添加数值
    model.addAttribute("point", 102920122);
    return "04-innerFunc";
  }

}
